package main

import (
	"fmt"
	"math"
)

const abortIndex int8 = math.MaxInt8 / 2

type MyContext struct {
	handlers []func(c *MyContext)
	index    int8
}

func New() *MyContext {
	return &MyContext{}
}
func (this *MyContext) Use(f func(c *MyContext)) {
	this.handlers = append(this.handlers, f)
}
func (this *MyContext) Abort() {
	this.index = abortIndex
}
func (this *MyContext) Get(path string, f func(c *MyContext)) {
	this.handlers = append(this.handlers, f)
}
func (this *MyContext) Next() {
	if this.index >= abortIndex {
			return
	}
	this.index++
	this.handlers[this.index](this)
}
func (this *MyContext) Run() {
	this.handlers[0](this)
}

//中间件1
func Middlerware1() func(c *MyContext) {
	return func(c *MyContext) {
		fmt.Println("mid-1 start")
		c.Next()
		fmt.Println("mid-1 end")
	}
}

//中间件2
func Middlerware2() func(c *MyContext) {
	return func(c *MyContext) {
		c.Abort()
		fmt.Println("mid-2 start")
		c.Next()
		fmt.Println("mid-2 end")
	}
}

//中间件3
func Middlerware3() func(c *MyContext) {
	return func(c *MyContext) {

		fmt.Println("mid-3 start")
		c.Next()
		fmt.Println("mid-3 end")
	}
}
